package timing.ukulele.third.service.msg;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import timing.ukulele.data.third.data.msg.ChannelAccountDTO;
import timing.ukulele.data.third.view.msg.ChannelAccountVO;
import timing.ukulele.third.convertor.msg.ChannelAccountConverter;
import timing.ukulele.third.exception.ThirdServiceException;
import timing.ukulele.third.mapper.msg.ChannelAccountMapper;
import timing.ukulele.third.msgChannel.model.account.BaseAccount;
import timing.ukulele.third.persistent.msg.ChannelAccount;
import timing.ukulele.third.persistent.msg.ChannelAccountGroup;
import timing.ukulele.third.service.common.BaseOperatorService;
import java.util.List;

/**
 * @Description 概要描述
 * @Author zh
 * @Date 2025-10-13 16:38
 */
@Service
public class ChannelAccountService extends BaseOperatorService<ChannelAccountMapper, ChannelAccount> {

    private final ChannelAccountGroupService channelAccountGroupService;

    public ChannelAccountService(ChannelAccountGroupService channelAccountGroupService) {
        this.channelAccountGroupService = channelAccountGroupService;
    }

    public Boolean addChannelAccount(ChannelAccountDTO dto) {
        validateWriteOperation(dto);
        ChannelAccount channelAccount = ChannelAccountConverter.INSTANCE.d2p(dto);
        return this.save(channelAccount);
    }

    private void validateWriteOperation(ChannelAccountDTO dto) {
        if (this.lambdaQuery()
                .eq(ChannelAccount::getName, dto.getName())
                .notIn(dto.getId() != null, ChannelAccount::getId, dto.getId()).count() > 0) {
            throw new ThirdServiceException("名称已存在，请勿重复添加");
        }
        ChannelAccountGroup channelAccountGroup = channelAccountGroupService.getById(dto.getGroupId());
        if (channelAccountGroup != null) {
            dto.setGroupName(channelAccountGroup.getName());
        }
        // 处理真实账号配置字段
        List<BaseAccount> baseAccounts = JSONArray.parseArray(dto.getAccountConfig(), BaseAccount.class);
        JSONObject jsonObject = JSON.parseObject(baseAccounts.getFirst().getAccountConfig());
        dto.setTrueAccountConfig(jsonObject.toString());
    }

    public Boolean updateChannelAccount(ChannelAccountDTO dto) {
        validateWriteOperation(dto);
        ChannelAccount channelAccount = ChannelAccountConverter.INSTANCE.d2p(dto);
        return this.updateById(channelAccount);
    }

    public Boolean deleteChannelAccount(Long id) {
        return this.lambdaUpdate().set(ChannelAccount::getDeleted, Boolean.TRUE)
                .eq(ChannelAccount::getId, id).update();
    }

    /**
     * 分页列表
     * @param dto
     * @return
     */
    public IPage<ChannelAccountVO> pageList(ChannelAccountDTO dto) {
        Page pageParam = new Page(dto.getCurrent(), dto.getPageSize());
        Page page = this.lambdaQuery()
                .eq(ChannelAccount::getGroupId, dto.getGroupId())
                .like(StrUtil.isNotBlank(dto.getName()), ChannelAccount::getName, dto.getName())
                .page(pageParam);
        List list = ChannelAccountConverter.INSTANCE.p2vList(page.getRecords());
        page.setRecords(list);
        return page;
    }

    /**
     * 根据渠道获取账号列表
     * @param channelType
     * @return
     */
    public List<ChannelAccount> findListBySendChannel(Integer channelType) {
        return this.lambdaQuery()
                .eq(ChannelAccount::getSendChannel, channelType)
                .eq(ChannelAccount::getDeleted, Boolean.FALSE)
                .list();
    }

    /**
     * 获取全部列表
     * @return
     */
    public List<ChannelAccount> getAllList() {
        return this.lambdaQuery()
                .eq(ChannelAccount::getDeleted, Boolean.FALSE)
                .list();
    }

}
